namespace Stock.WinForm
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Globalization;
    using System.Windows.Forms;
    using Stock.Bl;
    using Stock.Model;

    public partial class SumWeek : Form
    {
        public SumWeek()
        {
            InitializeComponent();

            DataGridViewColumn dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.Width = 120;
            dgvc.DataPropertyName = "Opt_Date";
            dgvc.HeaderText = "计算日期";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dgvc.HeaderCell.Style.BackColor = Color.SteelBlue;
            dgvc.HeaderCell.Style.ForeColor = Color.LightGray;

            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.Width = 90;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "银行利率";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.Width = 90;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "预订利率0";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.Width = 90;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "预订利率1";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.Width = 90;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "预订利率2";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.Width = 90;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "预订利率3";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvc.Width = 100;
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvc.DefaultCellStyle.Font = new Font("楷体_GB2312", 12F, FontStyle.Italic);
            dgvc.DefaultCellStyle.ForeColor = Color.DarkViolet;

            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "银行金额";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvc.Width = 100;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "预订金额0";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvc.Width = 100;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "预订金额1";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvc.Width = 100;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "预订金额2";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvc.Width = 100;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "预订金额3";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dgvc = new DataGridViewTextBoxColumn();
            dgvc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvc.DefaultCellStyle.Font = new Font("华文新魏", 10.5F, FontStyle.Bold);
            dgvc.DefaultCellStyle.ForeColor = Color.DarkGreen;
            dgvc.Width = 140;
            dgvc.DataPropertyName = "Opt_Code";
            dgvc.HeaderText = "实际金额";
            dgvc.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvc.HeaderCell.Style.WrapMode = DataGridViewTriState.False;
            dataGridView1.Columns.Add(dgvc);

            dataGridView1.Columns[5].Visible = false;
            dataGridView1.Columns[10].Visible = false;

            foreach (DataGridViewColumn cdd in dataGridView1.Columns)
            {
                cdd.SortMode = DataGridViewColumnSortMode.NotSortable;
            }
            dataGridView1.EnableHeadersVisualStyles = false;
            dataGridView1.Columns[0].HeaderCell.Style.ForeColor = Color.Yellow;
            dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.SteelBlue;
            dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = SystemColors.Info;
            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("楷体_GB2312", 14F, FontStyle.Regular);
            dataGridView1.ColumnHeadersHeight = 60;

            dataGridView1.DefaultCellStyle.BackColor = Color.Gainsboro;

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView1.AllowUserToResizeRows = false;

            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = System.Drawing.Color.Lavender;//System.Drawing.Color.FromArgb(60, 250, 240, 230);
            //dataGridView1.AllowUserToOrderColumns = false;
            //dataGridView1.AlternatingRowsDefaultCellStyle.SelectionBackColor = System.Drawing.Color.Fuchsia;
        }

        /// <summary>
        /// 限制输入为整数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void input_Integer_Press(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
        }

        /// <summary>
        /// 限制输入为浮点数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void input_float_Press(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }

            if (e.KeyChar == 46)                       //小数点
            {
                string txtValue = ((System.Windows.Forms.TextBox)sender).Text;

                if (txtValue.Length <= 0)
                    e.Handled = true;           //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(txtValue, out oldf);
                    b2 = float.TryParse(txtValue + e.KeyChar.ToString(), out f);
                    if (b2 == true)
                    {
                        if (b1 == false)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }

            if (e.KeyChar == 45)                       //负数
            {
                string txtValue = ((System.Windows.Forms.TextBox)sender).Text;

                if (txtValue.Length > 0)
                    e.Handled = true;           //只能是第一位
                else
                {
                    e.Handled = false;
                }
            }
        }

        /// <summary>
        ///  初期装载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SumWeek_Load(object sender, System.EventArgs e)
        {
            this.RefreshGridView();

            SetCombox();
        }

        private void SetCombox()
        {
            comboBox1.Items.Clear();
            SumWeekBll SumWeekBll = new SumWeekBll();
            List<DateTime> listDate = SumWeekBll.getFridayList();
            foreach (DateTime dt in listDate)
            {
                comboBox1.Items.Add(dt.ToString("yyyy-MM-dd"));
            }
            if (comboBox1.Items.Count < 1)
            {
                btnSave.Enabled = false;
            }
            else
            {
                comboBox1.SelectedIndex = 0;
            }
        }

        private void RefreshGridView()
        {
            try
            {
                dataGridView1.Rows.Clear();
                SumWeekBll sumWeekBll = new SumWeekBll();
                List<GainHisEntity> lstData = sumWeekBll.getGainHisList();

                int i = 0;
                //decimal decFee;
                //decimal feeSum = 0m; ;
                foreach (GainHisEntity sohe in lstData)
                {
                    dataGridView1.Rows.Add(new string[] { sohe.CalcDate, sohe.BankRate.ToString("0.00%")
                    , sohe.Pre0.ToString("0.00%"), sohe.Pre1.ToString("0.00%"), sohe.Pre2.ToString("0.00%")
                    , sohe.Pre3.ToString("0.00%"),sohe.BankAmount.ToString("#,##0.00")
                    ,sohe.Pre0Amount.ToString("#,##0.00"),sohe.Pre1Amount.ToString("#,##0.00")
                    ,sohe.Pre2Amount.ToString("#,##0.00"),sohe.Pre3Amount.ToString("#,##0.00"),
                    sohe.FactSumAmount.ToString("#,##0.00")});

                    if (sohe.FactSumAmount > sohe.Pre2Amount)
                    {
                        dataGridView1.Rows[i].Cells[11].Style.ForeColor = Color.Red;
                    }
                    else if (sohe.FactSumAmount > sohe.Pre1Amount)
                    {
                        dataGridView1.Rows[i].Cells[11].Style.ForeColor = Color.Magenta;
                    }
                    else if (sohe.FactSumAmount > sohe.BankAmount)
                    {
                        dataGridView1.Rows[i].Cells[11].Style.ForeColor = Color.LightPink;
                    }

                    i++;
                }
            }
            catch { }
        }

        private string formateDecimal(decimal? decValue)
        {
            if (decValue == null)
            {
                return "";
            }
            else
            {
                return ((decimal)decValue).ToString("#,##0.000");
            }
        }

        private string formateInt(int? decValue)
        {
            if (decValue == null)
            {
                return "";
            }
            else
            {
                return ((int)decValue).ToString("#,###");
            }
        }

        /// <summary>
        /// 设定生成的Combobox
        /// </summary>
        /// <param name="comboBox">Combobox对象</param>
        /// <param name="p">有无空白行</param>
        private void setComboBox(ComboBox comboBox, bool p)
        {
            StockOptDetailBl stockOptDetailBl = new StockOptDetailBl();
            DataSet dsList = stockOptDetailBl.GetTypeDataList("Parameter_Type='1'");
            DataTable dt = dsList.Tables[0];
            if (p)
            {
                System.Data.DataRow drNon = dt.NewRow();
                drNon["Parameter_Value"] = "";
                drNon["Parameter_Name"] = "";
                dt.Rows.InsertAt(drNon, 0);
            }

            if (dt != null)
            {
                comboBox.DataSource = dt;//绑定数据.
                comboBox.DisplayMember = "Parameter_Name";//显示项
                comboBox.ValueMember = "Parameter_Value";//值项
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, System.EventArgs e)
        {
            this.RefreshGridView();
        }

        /// <summary>
        /// 清理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClear_Click(object sender, System.EventArgs e)
        {
        }

        private decimal setEntityValue(TextBox tx)
        {
            decimal toValue = 0;
            if (tx.Text.Trim().Length > 0)
            {
                toValue = decimal.Parse(tx.Text.Trim());
            }
            return toValue;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, System.EventArgs e)
        {
            try
            {
                SumWeekBll sumWeekBll = new SumWeekBll();

                DateTime ddt = DateTime.ParseExact(comboBox1.SelectedItem.ToString(), "yyyy-MM-dd", CultureInfo.InvariantCulture);
                Rate rate = new Rate();
                rate.BankRate = decimal.Parse(txtBankRate.Text);
                rate.Pre0Rate = decimal.Parse(this.txtPre0Rate.Text);
                rate.Pre1Rate = decimal.Parse(this.txtPre1Rate.Text);
                rate.Pre2Rate = decimal.Parse(this.txtPre2Rate.Text);
                rate.Pre3Rate = decimal.Parse(this.txtPre3Rate.Text);

                DateTime startDate = sumWeekBll.getStartDate();

                sumWeekBll.saveCalcResult(startDate, ddt, rate);
                SetCombox();
                RefreshGridView();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGridView1_DoubleClick(object sender, System.EventArgs e)
        {
            if (dataGridView1.SelectedRows[0].Cells[8].Value != null && dataGridView1.SelectedRows[0].Cells[8].Value.ToString() != string.Empty)
            {
            }
        }
    }
}